<?php
namespace app\store\model;


use think\Db;
use think\Model;
use think\Session;

class Merchant extends Model
{
    /**
     * 商户订单列表
     * @param $dataType
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function userList($dataType, $query = []){

        // 检索查询条件
        !empty($query) && $this->setWhere($query);
        $list = Db::name('merchant')->alias('merchant')
            ->where($this->user())
            ->join('user', 'user.user_id = merchant.user_id')
            ->join('ticket', 'ticket.id = merchant.ticket_ids')
            ->join('scenic','ticket.scenic_id = scenic.id')
            ->join('UploadFile' , 'UploadFile.file_id = scenic.logo_image_id')
            ->where($this->transferDataType($dataType))
            ->field(
                'merchant.id,merchant.order_no,merchant.transaction_no,pay_type,
                merchant.user_id,merchant.store_user_id,merchant.article_price,merchant.num,merchant.ticket_ids,
                merchant.start_time,merchant.end_time,merchant.order_status,merchant.create_time,user.nickName,
                ticket.name,UploadFile.file_name'
            )
            ->order(['merchant.create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);
        return $list;
    }

    /**
     * 判断是否为超级管理员
     * @return array|bool
     */
    public function user(){
        $Session = Session::get('yoshop_store');
        if( $Session['user']['store_user_id'] == '10001' ){
            return true;
        } else if( $Session['user']['store_user_id'] != '10001'){
            $user = ['merchant.store_user_id' => $Session['user']['merchant_id']];
        }
        return $user;
    }

    /**
     * 商户订单详情列表
     * @param $id
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id){
        $data = Db::name('merchant')->where('merchant.id',$id)
            ->alias('merchant')
            ->join('user', 'user.user_id = merchant.user_id')
            ->join('ticket', 'ticket.id = merchant.ticket_ids')
            ->join('scenic','ticket.scenic_id = scenic.id')
            ->join('UploadFile' , 'UploadFile.file_id = scenic.logo_image_id')
            ->select();
        return $data;
    }

    private function setWhere($query)
    {
        if (isset($query['start_time']) && !empty($query['start_time'])) {
            $this->where('orders.start_time', '>=', strtotime($query['start_time']));
        }
        if (isset($query['end_time']) && !empty($query['end_time'])) {
            $this->where('orders.end_time', '<', strtotime($query['end_time']) + 86400);
        }
        // 用户id
        if (isset($query['user_id']) && $query['user_id'] > 0) {
            $this->where('orders.user_id', '=', (int)$query['user_id']);
        }
    }

    private function transferDataType($dataType)
    {
        // 数据类型
        $filter = [];
        switch ($dataType) {
            case 'all':
                $filter = [];
                break;
            case 'Write_off':
                $filter = ['order_status' => 200];
                break;
            case 'cancel':
                $filter = ['order_status' => 201];
                break;
            case 'confirm':
                $filter = ['order_status' => 300];
                break;
            case 'evaluate':
                $filter = ['order_status' => 400];
                break;
            case 'end':
                $filter = ['order_status' => 500];
                break;
            case 'complete':
                $filter = ['order_status' => 600];
                break;
        }
        return $filter;
    }

}